package com.yiqi.zhiyuan.feature.certification.presenter;

import android.text.TextUtils;

import com.yiqi.zhiyuan.entity.bo.UploadCertificatesInfo;
import com.yiqi.zhiyuan.entity.dto.DiverCertificationDTO;
import com.yiqi.zhiyuan.feature.certification.contract.UploadContract;
import com.yiqi.zhiyuan.feature.certification.dependencies.upload.DaggerUploadPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class UploadPresenter implements UploadContract.Presenter {

    private UploadContract.View mView;

    @Inject
    UploadContract.Model mModel;


    public UploadPresenter(UploadContract.View mView) {
        this.mView = mView;
        DaggerUploadPresenterComponent.create().inject(this);
    }


    @Override
    public void start() {

    }

    @Override
    public void uploadCertificate(List<UploadCertificatesInfo> mCertificatesInfos, DiverCertificationDTO certificationDTO) {
        for (int i = 0; i < mCertificatesInfos.size(); i++) {
            if (TextUtils.isEmpty(mCertificatesInfos.get(i).getImgUrl())) {
                mView.showUploadCertificateError(String.format("请上传%s", mCertificatesInfos.get(i).getContent()));
                return;
            }
        }
        mModel.doUploadCertificate(mCertificatesInfos,certificationDTO)
                .compose(mView.<String>bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showUploadCertificateSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showUploadCertificateError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });

    }


}
